package com.example.semo.controller;

import com.example.semo.dao.LoveDao;
import com.example.semo.entity.Love;
import com.example.semo.entity.Params;
import com.example.semo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@ResponseBody
public class LoveController {
    String msg;
    int code;
    @Autowired
    LoveDao loveDao;
    @RequestMapping("/selectLove")
    public JsonResult selectLove(@RequestBody Love love){
        System.out.printf(""+love.getUserId());
        List list=loveDao.SelectLove(love);
        if(list==null||list.size()==0){
            code=500;
            msg="数据为空";
        }else {
            code=200;
            msg="收藏列表查询成功";
        }
        return new JsonResult(code,msg,list);
    }
    @RequestMapping("/addLove")
    public JsonResult addLove(@RequestBody Love love){
        System.out.printf(""+love.getUserId());
        int i=loveDao.AddLove(love);
        if(i==0){
            code=500;
            msg="收藏失败";
        }else {
            code=200;
            msg="收藏成功";
        }
        return new JsonResult(code,msg,i);
    }
    @RequestMapping("/delLove")
    public JsonResult delLove(@RequestBody Love love){
        System.out.printf(""+love.getUserId());
        int i=loveDao.DelLove(love);
        if(i==0){
            code=500;
            msg="取消收藏失败";
        }else {
            code=200;
            msg="取消收藏成功";
        }
        return new JsonResult(code,msg,i);
    }
    @RequestMapping("/selectImgLove")
    public JsonResult selectImgLove(@RequestBody Params params){
        params.setPageNo((params.getPageNo()-1) * params.getPageSize());
        List list =loveDao.SelectImgLove(params);
        int count=list.size();
        list.add(count);
        if(null == list || list.size()==0){
            code=500;
            msg="数据为空";
        }else{
            code=200;
            msg="查询收藏列表图片成功";
        }
        return new JsonResult(200,msg, list);
    }
}
